﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using log4net;

namespace ExcelReportGeneratorFormApp
{
    public partial class AppForm : Form
    {
        // Create a logger
        ILog log = LogManager.GetLogger(typeof(Program));

        public AppForm()
        {
            InitializeComponent();

            log.Info("Load form");

            //Get the configuration file
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            textBoxTemplateExcel.Text = config.AppSettings.Settings["TemplateExcel"].Value;
            textBoxOutputPathExcel.Text = config.AppSettings.Settings["OutputPathExcel"].Value;
            textBoxOutputPathPdf.Text = config.AppSettings.Settings["OutputPathPdf"].Value;
            textBoxLimit.Text = config.AppSettings.Settings["Limit"].Value;
        }

        private void buttonSaveSettings_Click(object sender, EventArgs e)
        {
            log.Info("Save app settings");

            //Get the configuration file
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings["TemplateExcel"].Value = textBoxTemplateExcel.Text;
            config.AppSettings.Settings["OutputPathExcel"].Value = textBoxOutputPathExcel.Text;
            config.AppSettings.Settings["OutputPathPdf"].Value = textBoxOutputPathPdf.Text;
            config.AppSettings.Settings["Limit"].Value = textBoxLimit.Text;

            config.AppSettings.SectionInformation.ForceSave = true;

            //Save the configuration file
            config.Save(ConfigurationSaveMode.Modified);

            //force the reload of the changed section
            ConfigurationManager.RefreshSection("appSettings");
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            log.Info("Generate reports");

            MessageBox.Show("Start de rapportage door op OK te klikken.\nZie daarna de log (ExcelReportGenerator.log) voor de details!");

            new ExcelApp(checkBoxSaveExcel.Checked, checkBoxSavePdf.Checked).Run();

            MessageBox.Show("De rapportage is gereed.\nZie de log (ExcelReportGenerator.log) voor de details!");
        }
    }
}
